package dk.dashboardFP.panels.displayPanel {
	import dk.dashboardFP.utils.DisplayChainTools;

	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	/**
	 * @author josef
	 */
	public class DragController extends EventDispatcher {
		
		private var _oDisplayPanel : DisplayPanel;
		private var _doStage : Stage;
		private var _pCurrentDrag : Point;
		private var _pStartPoint : Point;

		public function DragController(oDisplayPanel:DisplayPanel) {
			_oDisplayPanel = oDisplayPanel;
			
			super();
			setupElements();
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// setup
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		private function setupElements() : void {
			_doStage=_oDisplayPanel.doDashboard.doStage;
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// interface
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		public function enableDrag() : void {
			_doStage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseEvent, true, 0, false);
			_doStage.addEventListener(MouseEvent.MOUSE_UP, onMouseEvent, true, 0, false);
			_doStage.addEventListener(MouseEvent.MOUSE_OVER, onMouseEvent, true, 0, false);
			//_doStage.addEventListener(MouseEvent.CLICK, onMouseEvent, true, 0, false);
		}

		public function disableDrag() : void {
			_doStage.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseEvent, true);
			_doStage.removeEventListener(MouseEvent.MOUSE_UP, onMouseEvent, true);
			_doStage.removeEventListener(MouseEvent.MOUSE_OVER, onMouseEvent, true);
			//_doStage.removeEventListener(MouseEvent.CLICK, onMouseEvent, true);
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// drag control
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		private function onMouseEvent(event : MouseEvent) : void {

			//:::::::
			// return if child of panel 
			if (DisplayChainTools.get_bIsChildOf(DisplayObject(event.target), _oDisplayPanel.doDashboard.doPanelHolder)) return;
			event.stopImmediatePropagation();
			
			if (event.type==MouseEvent.MOUSE_DOWN) {
				startDrag();
			} else if (event.type==MouseEvent.MOUSE_UP) {
				endDrag();
			}
		}

		private function endDrag() : void {
			dispatchEvent(new DragControllerEvent(DragControllerEvent.END_DRAG));
		}

		private function startDrag() : void {
			
			//:::::::
			// setup drag 
			_doStage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			_pStartPoint=new Point(_doStage.mouseX, _doStage.mouseY);
			_pCurrentDrag=new Point();
			
			dispatchEvent(new DragControllerEvent(DragControllerEvent.START_DRAG));
		}

		private function onMouseMove(event : MouseEvent) : void {
			_pCurrentDrag.x = _doStage.mouseX - _pStartPoint.x;
			_pCurrentDrag.y = _doStage.mouseY - _pStartPoint.y;
			dispatchEvent(new DragControllerEvent(DragControllerEvent.UPDATE_DRAG));
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// getters
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		public function get pCurrentDrag() : Point {
			return _pCurrentDrag;
		}
	}
}
